const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        schedule: 0,
        info: {},
        checkNum: 0,
        used_id: 0,
        timer: '',
    },
    onLoad(options) {
        my.showLoading({
            content: 'loading...'
        });
        let that = this;
        that.setData({
            info: JSON.parse(options.data)
        });
        // this.checkUsed(1);
        this.checkUsed(that.data.info.used_id);
        my.hideLoading();
    },
    // 页面隐藏/切入后台时触发。 如 my.navigateTo 到其他页面或底部 tab 切换等
    onHide() {
        clearInterval(this.data.timer);
    },
    // 页面卸载时触发。 如 my.redirectTo 或 my.navigateBack 到其他页面等。
    onUnload() {
        clearInterval(this.data.timer);
    },
    // ==================== 校验是否借出成功 =======================
    checkUsed(used_id) {
        let that = this;
        let check = that.data.checkNum;
        let schedule = that.data.schedule;
        check++;
        schedule = schedule + 4;
        that.setData({
            checkNum: check,
            schedule: schedule
        });
        if (that.data.checkNum < 25) {
            util.httpRequest('/checkUsed', {
                used_id: used_id
            }, function (res) {
                that.setData({
                    used_id: res.data.used_id
                });
                if (res.data.status == 1) {
                    // 租借成功，跳转借用成功界面
                    that.setData({
                        schedule: 100
                    });
                    my.reLaunch({
                        url: '../borrowStatus/borrowStatus?used_id=' + res.data.used_id + "&borrow_status=1"
                    })
                } else {
                    var timerTem = setTimeout(() => {
                        that.checkUsed(used_id);
                    }, 3000);
                    that.setData({
                        timer: timerTem
                    });
                }
            }, function () {
                my.hideLoading();
            })
            // setTimeout(() => {
            //   that.checkUsed(used_id);
            // }, 3000);
        } else {
            // 时间已经走完，还是租借失败，跳转到失败页面
            my.reLaunch({
                url: '../borrowStatus/borrowStatus?borrow_status=0&used_id=' + that.data.used_id
            })
        }
    }
});
